package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Report;
import com.itheima.pojo.RiskAssessmentReport;
import com.itheima.pojo.RiskParams;
import com.itheima.service.RiskAssessmentService;
import com.itheima.utils.CookiesUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author ZS
 */
@RestController
@RequestMapping("riskAssessment")
public class RiskAssessmentController {

    @Reference(version = "1.0.0")
    private RiskAssessmentService riskAssessmentService;

    /**
     *  根据fileNumber查找体检报告
     * @param fileNumber
     * @return result
     */
    @GetMapping("findReportByFileNumber")
    public Result findReportByFileNumber(String fileNumber){
        return riskAssessmentService.findReportByFileNumber(fileNumber);
    }

    @PostMapping("addRiskAssessment")
    public Result addRiskAssessment(@RequestBody RiskParams params){
        String fileNumber = params.getFileNumber();
        List<Report> report = params.getReport();
        return   riskAssessmentService.addRiskAssessment(fileNumber, report);
    }


    /**
     * 根据ID查询报告
     * @return  返回结果 list
     */
    @GetMapping("mobileFindRiskReportByFileNumber")
//    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public Result findById(HttpServletRequest requests){
        String fileNumber = CookiesUtils.getCookie(requests, "login_member_fileNumber");
        Result reportByFileNumber = riskAssessmentService.findReportByFileNumber(fileNumber);
        Object report = reportByFileNumber.getData();
        if(report == null){
            return new Result(false,"还未进行体检，无法进行体检风险评估");
        }
        List<RiskAssessmentReport> list = riskAssessmentService.findById(fileNumber);
        return new Result(true,"查询成功",list);
    }


    /**
     * 删除
     * @param fileNumber 要删除的风评的fileNumber
     * @return  删除成功
     */
    @GetMapping("delete/{fileNumber}")
//    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    public Result delete(@PathVariable String fileNumber){
        //调用业务层删除方法
        riskAssessmentService.delete(fileNumber);
        //返回操作结果
        return new Result(true, "删除风评成功");
    }


    /**
     * 分页查询方法
     * @param queryPageBean 分页参数实体类
     * @return  分页查询结果，总条数与当前页数据
     */
    @RequestMapping("findPage")
//    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = riskAssessmentService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }
}
